题目:
Given the root
of a binary tree, return the preorder traversal of its nodes' values.
Example 1:
Input: root = [1,null,2,3]
Output: [1,2,3]
Example 2:
Input: root = []
Output: []
Example 3:
Input: root = [1]
Output: [1]
Example 4:
Input: root = [1,2]
Output: [1,2]
Example 5:
Input: root = [1,null,2]
Output: [1,2]
Constraints:
- The number of nodes in the tree is in the range
[0, 100]
. -100 <= Node.val <= 100
就,简单的前序遍历。
2023.1.5
还是放一个递归方法在这吧。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
preorder(root, result);
return result;
}
private void preorder(TreeNode root, List<Integer> result) {
if (root == null) {
return;
}
result.add(root.val);
preorder(root.left, result);
preorder(root.right, result);
}
}
递归方法就不说了。直接来说迭代的方法。在复习了inorder(LeetCode 94. Binary Tree Inorder Traversal_wenyq7的博客-CSDN博客)以后自己在草稿纸上比比画画居然就写出了inorder里我觉得比较难理解的那种不用helper function的写法。在大while loop里,首先是在使劲儿往左找的时候要一边往stack里push,一边记录在result里,找到最左以后停下,把stack top pop出来就是最左,再把它的右子节点设为curr,继续while loop。这里就很自然地想到while loop的判断条件应该是stack非空或者curr != null了,stack非空对应右子节点为null的情况,也还得继续,curr != null对应开始的根结点以及根结点的右子节点。时间复杂度O(n),空间O(n)。
Runtime: 0 ms, faster than 100.00% of Java online submissions for Binary Tree Preorder Traversal.
Memory Usage: 37.2 MB, less than 98.98% of Java online submissions for Binary Tree Preorder Traversal.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
Deque<TreeNode> stack = new ArrayDeque<>();
TreeNode curr = root;
while (!stack.isEmpty() || curr != null) {
while (curr != null) {
result.add(curr.val);
stack.push(curr);
curr = curr.left;
}
curr = stack.pop();
curr = curr.right;
}
return result;
}
}
然后看了lc solution才发现我把问题复杂化了。其实非常简单。直接先把root加进stack,然后while stack不为空,stack.pop()就是父节点,然后如果这个节点有做左子节点或者右子节点,直接把这俩节点也push进stack,注意是先push右再push左,因为stack pop的时候是反过来的。但是这个做法因为最开始就加入root,所以没办法包括root为null的情况,需要特殊判断(掉坑了)。
Runtime: 0 ms, faster than 100.00% of Java online submissions for Binary Tree Preorder Traversal.
Memory Usage: 37.3 MB, less than 98.91% of Java online submissions for Binary Tree Preorder Traversal.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
if (root == null) {
return new ArrayList<>();
}
List<Integer> result = new ArrayList<>();
Deque<TreeNode> stack = new ArrayDeque<>();
stack.add(root);
while (!stack.isEmpty()) {
TreeNode curr = stack.pop();
result.add(curr.val);
if (curr.right != null) {
stack.push(curr.right);
}
if (curr.left != null) {
stack.push(curr.left);
}
}
return result;
}
}
2023.1.6
写了上面的第二种迭代方法,刚开始还写错了,两个if之间的逻辑关系没想清楚,第二个用成了else if……这里直接并列if就行,因为push进去的时候就在stack bottom了,会先把顶上的left们都遍历一遍再回去。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
if (root == null) {
return result;
}
Deque<TreeNode> stack = new ArrayDeque<>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
result.add(node.val);
if (node.right != null) {
stack.push(node.right);
}
if (node.left != null) {
stack.push(node.left);
}
}
return result;
}
}