【LeetCode从零单刷】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

都是利用这个工具完成树的遍历。不过这道题中,因为第一个被访问的是左子树,所以栈中保存的是根节点

保存子树节点与根节点的不同在于:根节点灵活性小,子树节点灵活性较大,判断是否到达树底更复杂。例如:

  1. 【1,2,3,#,4】此时的 2 节点没有左子树,但是不能直接进入栈中元素的右子树,因为其本身还有一个右子树;
  2. 【1,2,#,3】此时只有左子树,根本无法进入右子树,就栈不断返回元素。
class Solution {
public:
    vector<int> inorderTraversal(TreeNode *root) {
        vector<int> result;
        if(root == NULL)    return result;

        stack<TreeNode*> rootStack;
        TreeNode* tmp = root;
        bool tag = true;
        while(tag || !rootStack.empty())
        {
            while(tmp->left)
            {
                rootStack.push(tmp);
                tmp = tmp->left;
            }
            result.push_back(tmp->val);

            // 如果不是叶子节点,有右子树
            if(tmp->right)
                tmp = tmp->right;

            // 没有左子树且没有右子树,一定是叶子节点
            else if(!rootStack.empty())
            {
                tmp = rootStack.top();
                rootStack.pop();
                result.push_back(tmp->val);

                // 如果没有右子树,不断回溯
                while(tmp->right == NULL)
                {
                    if(rootStack.empty())
                    {
                        tag = false;
                        break;
                    }
                    else
                    {
                        tmp = rootStack.top();
                        result.push_back(tmp->val);
                        rootStack.pop();
                    }
                }
                if(tmp->right)
                {
                    tmp = tmp->right;
                }
            }
            else
            {
                tag = false;
            }
        }

        return result;
    }
};
时间: 2024-10-16 09:39:52

【LeetCode从零单刷】Binary Tree Inorder Traversal的相关文章

LeetCode(94)Binary Tree Inorder Traversal

题目如下: Python代码: def inorderTraversal(self, root): res = [] self.helper(root, res) return res def helper(self, root, res): if root: self.helper(root.left, res) res.append(root.val) self.helper(root.right, res)

leetcode -day29 Binary Tree Inorder Traversal &amp; Restore IP Addresses

1.  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 Inorder Traversal

LeetCode解题之Binary Tree Inorder Traversal 原题 不用递归来实现树的中序遍历. 注意点: 无 例子: 输入: {1,#,2,3} 1 2 / 3 输出: [1,3,2] 解题思路 通过栈来实现,从根节点开始,不断寻找左节点,并把这些节点依次压入栈内,只有在该节点没有左节点或者它的左子树都已经遍历完成后,它才会从栈内弹出,这时候访问该节点,并它的右节点当做新的根节点一样不断遍历. AC源码 # Definition for a binary tree node

[LeetCode][JavaScript]Binary Tree Inorder Traversal

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? https://leetcode.

leetcode - Binary Tree Preorder Traversal &amp;&amp; Binary Tree Inorder Traversal &amp;&amp; Binary Tree Postorder Traversal

简单来说,就是二叉树的前序.中序.后序遍历,包括了递归和非递归的方法 前序遍历(注释中的为递归版本): 1 #include <vector> 2 #include <stack> 3 #include <stddef.h> 4 #include <iostream> 5 6 using namespace std; 7 8 struct TreeNode 9 { 10 int val; 11 TreeNode *left; 12 TreeNode *rig

LeetCode: Binary Tree Inorder Traversal [094]

[题目] 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? confused what "{1,#,2,3}" means? >

[leetcode]Binary Tree Inorder Traversal @ Python

原题地址:http://oj.leetcode.com/problems/binary-tree-inorder-traversal/ 题意:二叉树的中序遍历. 解题思路:这道题用递归解不难,所以应该考察的是非递归求解二叉树的中序遍历.我们使用一个栈来解决问题.比如一颗二叉树为{1,2,3,4,5,6,7},第一层为{1},第二层为{2,3},第三层为{4,5,6,7}.那么我们依次存储左子树的根节点,那么入栈顺序为:1,2,4.由于4的左子树为空,所以开始出栈.4出栈,检查4的右子树为空,继续

LeetCode——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? 原题链接:https://oj.leetcode.com/problems/binary-t

[Leetcode][Tree][Binary Tree Inorder Traversal]

二叉树的中序遍历 1.递归版本 /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: void dfsInorderTraversal(TreeNode *root, vect

Leetcode 树 Binary Tree Inorder Traversal

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