【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].


可用递归,进行。也可以用迭代。

C++:

 1 class Solution {
 2 public:
 3     void preorder(TreeNode * root, vector<int> &path)
 4     {
 5         if(root)
 6         {
 7             path.push_back(root->val);
 8             preorder(root->left,path);
 9             preorder(root->right,path);
10         }
11     }
12     vector<int> preorderTraversal(TreeNode *root) {
13         vector<int> path;
14         if(root==NULL) return path;
15         preorder(root,path);
16         return path;
17     }
18 };
 1 class Solution{
 2 public:
 3     vector<int> preorderTraversal(TreeNode *root){
 4         vector<int> path;
 5         stack<TreeNode*> stk;
 6         while(root!=NULL||!stk.empty())
 7         {
 8             if(root!=NULL)
 9             {
10                 while(root)
11                 {
12                     path.path_back(root->val);
13                     stk.push(root);
14                     root=root->left;
15                 }
16             }else{
17                 root=stk.top()->right;
18                 stk.pop();
19             }
20         }
21         return path;
22     }
23 };

Python:

 1 # Definition for a  binary tree node
 2 # class TreeNode:
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.left = None
 6 #         self.right = None
 7
 8 class Solution:
 9     # @param root, a tree node
10     # @return a list of integers
11     def preorderTraversal(self, root):
12         if root==None:
13             return []
14         if(root):
15             return [root.val]+self.preorderTraversal(root.left)+self.preorderTraversal(root.right)
16
17             
时间: 2024-10-13 13:07:58

【leetcode】Binary Tree Preorder Traversal的相关文章

【LeetCode】Binary Tree Preorder Traversal (2 solutions)

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? 解法一:递归 /** * De

【Leetcode】Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 思路:后序遍历比起先序遍历以及中序遍历要稍微复杂一点,可以考虑用两个stack进行操作,

【LeetCode】Binary Tree Inorder Traversal (2 solutions)

Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? 解法一:递归 /** * Defi

【LeetCode】Binary Tree Postorder Traversal (3 solutions)

Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 解法一:递归法 /** *

【Leetcode】【Medium】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]. 解题思路: 二叉树非递归先序遍历,使用栈来保存被遍历到的,但是还没遍历其右子树的结点. 代码: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode

【leetcode】Binary Tree Inorder Traversal

与前面的先序遍历相似. 此题为后序遍历. C++: 1 /** 2 * Definition for binary tree 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<i

【leetcode】Binary Tree Postorder Traversal (hard) ☆

二叉树的后序遍历 用标记右子树vector的方法 vector<int> postorderTraversal(TreeNode *root) { vector<int> ans; vector<TreeNode *> stack; vector<bool> isRight; stack.push_back(root); isRight.push_back(false); TreeNode * pNode = NULL; while(!stack.empty

【Leetcode】Binary Tree Level Order Traversal

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ] 思路:使用

【Leetcode】Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order trave