LeetCode 144. Binary Tree Preorder Traversal

前序遍历

递归:

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     vector<int> res;
13     void help(TreeNode* root){
14         // if(!root) return;
15         res.push_back(root->val);
16         if(root->left) help(root->left);
17         if(root->right) help(root->right);
18     }
19     vector<int> preorderTraversal(TreeNode* root) {
20         if(!root) return res;
21         help(root);
22         return res;
23     }
24
25
26 };

迭代:

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     vector<int> preorderTraversal(TreeNode* root){
13         vector<int> res;
14         if(!root) return res;
15         stack<TreeNode*> stk;
16         stk.push(root);
17
18         while(!stk.empty()){
19             TreeNode* cur = stk.top();
20             stk.pop();
21             res.push_back(cur->val);
22
23             if(cur->right) stk.push(cur->right);
24             if(cur->left) stk.push(cur->left);
25
26         }
27         return res;
28     }
29
30 };

stk存放待处理节点的倒序,即top为最先处理的节点。

每次处理完top后,将top的右、左儿子依次push进stk中,所以下一次先处理的是top的左儿子(再深一步想也即先处理top的左子树,在该子树中优先处理其中的左子树,以此类推),符合前序遍历的规则。

另有算法未看,留坑。

时间: 2024-11-03 03:29:32

LeetCode 144. Binary Tree Preorder Traversal的相关文章

LeetCode 144. Binary Tree Preorder Traversal 解题报告

144. Binary Tree Preorder Traversal My Submissions Question Total Accepted: 108336 Total Submissions: 278322 Difficulty: Medium Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3

LeetCode 144 Binary Tree Preorder Traversal (先序遍历二叉树)

Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 题目链接:https://leetcode.com/problems/binary-tre

Java for LeetCode 144 Binary Tree Preorder Traversal

Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. 二叉树的前序遍历,根节点→左子树→右子树 解题思路一: 递归实现,JAVA实现如下: public List<Integer> preorderTraversal(TreeNode root) { List<Int

Java [Leetcode 144]Binary Tree Preorder Traversal

题目描述: Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. 解题思路: 这个问题最简单的方法是使用递归,但是题目规定不能使用,得使用迭代的方法. 那么我们考虑使用栈来实现. 思路是每次遍历这节点,把该点的值放入list中,然后把该点的右孩子放入栈中,并将当前点设置为左孩子

leetcode 144. Binary Tree Preorder Traversal ----- java

Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 求前序遍历,要求不用递归. 使用双向队列. /** * Definition for a b

leetcode 144. Binary Tree Preorder Traversal 二叉树的前序遍历

前序遍历的递归解法: 方法一C++: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 vector<

LeetCode 144. Binary Tree Preorder Traversal 动态演示

先序遍历的非递归办法,还是要用到一个stack class Solution { public: vector<int> preorderTraversal(TreeNode* root) { vector<int> ret; if(!root) return ret; stack<TreeNode*> stk; stk.push(root); //ahd(root) //a(stk) //a(ret) while(stk.size()>0){ TreeNode*

[LeetCode 题解]: Binary Tree Preorder Traversal

Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 题意 先序遍历二叉树,递归的思路是普通的,能否用迭代呢? 非递归思路:<借助stack>

Leetcode 树 Binary Tree Preorder Traversal

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Binary Tree Preorder Traversal Total Accepted: 17948 Total Submissions: 51578 Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2