LeetCode - Easy - 144. Binary Tree Preorder Traversal

秒速五厘米 2022-10-17 00:35 184阅读 0赞

Topic

  • Stack
  • Tree

Description

https://leetcode.com/problems/binary-tree-preorder-traversal/

Given the root of a binary tree, return the preorder traversal of its nodes’ values.

Example 1:

93c03b302d4108b3debe3c473af634ea.png

  1. Input: root = [1,null,2,3]
  2. Output: [1,2,3]

Example 2:

  1. Input: root = []
  2. Output: []

Example 3:

  1. Input: root = [1]
  2. Output: [1]

Example 4:

428ab6a3e4c104cf4b41ad1a1a0a6f24.png

  1. Input: root = [1,2]
  2. Output: [1,2]

Example 5:

67437fd84487294f797443df319c90e9.png

  1. Input: root = [1,null,2]
  2. Output: [1,2]

Constraints:

  • The number of nodes in the tree is in the range [0, 100].
  • -100 <= Node.val <= 100

Follow up: Recursive solution is trivial, could you do it iteratively?

Analysis

方法一:递归法。

方法二:迭代法。

Submission

  1. import java.util.ArrayList;
  2. import java.util.LinkedList;
  3. import java.util.List;
  4. import com.lun.util.BinaryTree.TreeNode;
  5. public class BinaryTreePreorderTraversal {
  6. public List<Integer> preorderTraversal(TreeNode root) {
  7. List<Integer> result = new ArrayList<>();
  8. preorderTraversal(root, result);
  9. return result;
  10. }
  11. private void preorderTraversal(TreeNode root, List<Integer> result){
  12. if(root == null) return;
  13. result.add(root.val);
  14. preorderTraversal(root.left, result);
  15. preorderTraversal(root.right, result);
  16. }
  17. public List<Integer> preorderTraversal2(TreeNode root) {
  18. List<Integer> result = new ArrayList<>();
  19. LinkedList<TreeNode> stack = new LinkedList<>();
  20. if(root != null) stack.push(root);
  21. while(!stack.isEmpty()) {
  22. TreeNode node = stack.pop();
  23. result.add(node.val);
  24. if(node.right != null)
  25. stack.push(node.right);
  26. if(node.left != null)
  27. stack.push(node.left);
  28. }
  29. return result;
  30. }
  31. }

Test

  1. import static org.junit.Assert.*;
  2. import org.hamcrest.collection.IsEmptyCollection;
  3. import org.hamcrest.collection.IsIterableContainingInOrder;
  4. import org.junit.Test;
  5. import com.lun.util.BinaryTree;
  6. public class BinaryTreePreorderTraversalTest {
  7. @Test
  8. public void test() {
  9. BinaryTreePreorderTraversal obj = new BinaryTreePreorderTraversal();
  10. assertThat(obj.preorderTraversal(BinaryTree.integers2BinaryTree(1, null, 2, 3)), //
  11. IsIterableContainingInOrder.contains(1, 2, 3));
  12. assertThat(obj.preorderTraversal(null), IsEmptyCollection.empty());
  13. assertThat(obj.preorderTraversal(BinaryTree.integers2BinaryTree(1)), //
  14. IsIterableContainingInOrder.contains(1));
  15. assertThat(obj.preorderTraversal(BinaryTree.integers2BinaryTree(1,2)), //
  16. IsIterableContainingInOrder.contains(1,2));
  17. assertThat(obj.preorderTraversal(BinaryTree.integers2BinaryTree(1,null,2)), //
  18. IsIterableContainingInOrder.contains(1,2));
  19. }
  20. @Test
  21. public void test2() {
  22. BinaryTreePreorderTraversal obj = new BinaryTreePreorderTraversal();
  23. assertThat(obj.preorderTraversal2(BinaryTree.integers2BinaryTree(1, null, 2, 3)), //
  24. IsIterableContainingInOrder.contains(1, 2, 3));
  25. assertThat(obj.preorderTraversal2(null), IsEmptyCollection.empty());
  26. assertThat(obj.preorderTraversal2(BinaryTree.integers2BinaryTree(1)), //
  27. IsIterableContainingInOrder.contains(1));
  28. assertThat(obj.preorderTraversal2(BinaryTree.integers2BinaryTree(1,2)), //
  29. IsIterableContainingInOrder.contains(1,2));
  30. assertThat(obj.preorderTraversal2(BinaryTree.integers2BinaryTree(1,null,2)), //
  31. IsIterableContainingInOrder.contains(1,2));
  32. }
  33. }

发表评论

表情:
评论列表 (有 0 条评论,184人围观)

还没有评论,来说两句吧...

相关阅读