144. 二叉树的前序遍历
给你二叉树的根节点 root
,返回它节点值的 前序 遍历。
示例 1:
输入:root = [1,null,2,3] 输出:[1,2,3]
示例 2:
输入:root = [] 输出:[]
示例 3:
输入:root = [1] 输出:[1]
示例 4:
输入:root = [1,2] 输出:[1,2]
示例 5:
输入:root = [1,null,2] 输出:[1,2]
提示:
[0, 100]
内-100 <= Node.val <= 100
进阶:递归算法很简单,你可以通过迭代算法完成吗?
原站题解
golang 解法, 执行用时: 0 ms, 内存消耗: 1.9 MB, 提交时间: 2023-12-11 22:46:49
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ func preorderTraversal1(root *TreeNode) (vals []int) { var p1, p2 *TreeNode = root, nil for p1 != nil { p2 = p1.Left if p2 != nil { for p2.Right != nil && p2.Right != p1 { p2 = p2.Right } if p2.Right == nil { vals = append(vals, p1.Val) p2.Right = p1 p1 = p1.Left continue } p2.Right = nil } else { vals = append(vals, p1.Val) } p1 = p1.Right } return } func preorderTraversal(root *TreeNode) (vals []int) { stack := []*TreeNode{} node := root for node != nil || len(stack) > 0 { for node != nil { vals = append(vals, node.Val) stack = append(stack, node) node = node.Left } node = stack[len(stack)-1].Right stack = stack[:len(stack)-1] } return }
python3 解法, 执行用时: 44 ms, 内存消耗: 16.1 MB, 提交时间: 2023-12-11 22:46:03
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: # 递归 def preorderTraversal1(self, root: TreeNode) -> List[int]: def preorder(root: TreeNode): if not root: return res.append(root.val) preorder(root.left) preorder(root.right) res = list() preorder(root) return res # 迭代 def preorderTraversal2(self, root: TreeNode) -> List[int]: res = list() if not root: return res stack = [] node = root while stack or node: while node: res.append(node.val) stack.append(node) node = node.left node = stack.pop() node = node.right return res # morris遍历 def preorderTraversal(self, root: TreeNode) -> List[int]: res = list() if not root: return res p1 = root while p1: p2 = p1.left if p2: while p2.right and p2.right != p1: p2 = p2.right if not p2.right: res.append(p1.val) p2.right = p1 p1 = p1.left continue else: p2.right = None else: res.append(p1.val) p1 = p1.right return res
java 解法, 执行用时: 0 ms, 内存消耗: 40.1 MB, 提交时间: 2023-12-11 22:44:15
/** * 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> res = new ArrayList<Integer>(); preorder(root, res); return res; } public void preorder(TreeNode root, List<Integer> res) { if (root == null) { return; } res.add(root.val); preorder(root.left, res); preorder(root.right, res); } }
java 解法, 执行用时: 0 ms, 内存消耗: 39.9 MB, 提交时间: 2023-12-11 22:43:54
/** * 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> res = new ArrayList<Integer>(); if (root == null) { return res; } TreeNode p1 = root, p2 = null; while (p1 != null) { p2 = p1.left; if (p2 != null) { while (p2.right != null && p2.right != p1) { p2 = p2.right; } if (p2.right == null) { res.add(p1.val); p2.right = p1; p1 = p1.left; continue; } else { p2.right = null; } } else { res.add(p1.val); } p1 = p1.right; } return res; } public List<Integer> preorderTraversal2(TreeNode root) { List<Integer> res = new ArrayList<Integer>(); if (root == null) { return res; } Deque<TreeNode> stack = new LinkedList<TreeNode>(); TreeNode node = root; while (!stack.isEmpty() || node != null) { while (node != null) { res.add(node.val); stack.push(node); node = node.left; } node = stack.pop(); node = node.right; } return res; } }
cpp 解法, 执行用时: 4 ms, 内存消耗: 8.5 MB, 提交时间: 2023-12-11 22:42:57
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: vector<int> preorderTraversal(TreeNode *root) { vector<int> res; if (root == nullptr) { return res; } TreeNode *p1 = root, *p2 = nullptr; while (p1 != nullptr) { p2 = p1->left; if (p2 != nullptr) { while (p2->right != nullptr && p2->right != p1) { p2 = p2->right; } if (p2->right == nullptr) { res.emplace_back(p1->val); p2->right = p1; p1 = p1->left; continue; } else { p2->right = nullptr; } } else { res.emplace_back(p1->val); } p1 = p1->right; } return res; } };
cpp 解法, 执行用时: 4 ms, 内存消耗: 8.8 MB, 提交时间: 2023-12-11 22:42:39
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: vector<int> preorderTraversal(TreeNode* root) { vector<int> res; if (root == nullptr) { return res; } stack<TreeNode*> stk; TreeNode* node = root; while (!stk.empty() || node != nullptr) { while (node != nullptr) { res.emplace_back(node->val); stk.emplace(node); node = node->left; } node = stk.top(); stk.pop(); node = node->right; } return res; } };
cpp 解法, 执行用时: 0 ms, 内存消耗: 8.7 MB, 提交时间: 2023-12-11 22:42:22
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: void preorder(TreeNode *root, vector<int> &res) { if (root == nullptr) { return; } res.push_back(root->val); preorder(root->left, res); preorder(root->right, res); } vector<int> preorderTraversal(TreeNode *root) { vector<int> res; preorder(root, res); return res; } };
golang 解法, 执行用时: 0 ms, 内存消耗: 2 MB, 提交时间: 2021-07-22 10:10:43
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ func preorderTraversal(root *TreeNode) (ans []int) { var dfs func(*TreeNode) dfs = func(node *TreeNode) { if node != nil { ans = append(ans, node.Val) dfs(node.Left) dfs(node.Right) } } dfs(root) return ans }
golang 解法, 执行用时: 0 ms, 内存消耗: 2 MB, 提交时间: 2021-06-11 11:13:00
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ func preorderTraversal(root *TreeNode) (ans []int) { var inorder func(*TreeNode) inorder = func(node *TreeNode) { if node != nil { ans = append(ans, node.Val) inorder(node.Left) inorder(node.Right) } } inorder(root) return ans }