【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<int> inorderTraversal(TreeNode *root) {
13         vector<int>path;
14         stack<TreeNode*>stk;
15         while(root!=NULL||!stk.empty())
16         {
17             while(root!=NULL)
18             {
19                 stk.push(root);
20                 root=root->left;
21             }
22             if(!stk.empty())
23             {
24                 root=stk.top();
25                 path.push_back(root->val);
26                 stk.pop();
27                 root=root->right;
28             }
29         }
30         return path;
31     }
32 };

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 inorderTraversal(self, root):
12         if root is None:
13             return []
14         return self.inorderTraversal(root.left)+[root.val]+self.inorderTraversal(root.right)
时间: 2024-10-07 10:50:00

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

【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

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 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】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】【Python】Binary Tree Inorder Traversal

Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 比較简单,就是转化成中序遍历就可以.訪问顺序是中序遍历左子树.根节点,中序遍历右子树 Python编程的时候须要注意,要在返回单一数字的时候加上中括号[],否则Python不知道这是一个list # Definition for a binary tree node #

【Leetcode】【Medium】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? 解题思路: 二叉树中序非递归遍历,使用栈来保存遍历到的但是还没有访问其右子树元素的结点: 代码

【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  /3return [1,2,3]. 可用递归,进行.也可以用迭代. C++: 1 class Solution { 2 public: 3 void preorder(TreeNode * root, vector<int> &pat

【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] ] 思路:使用