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?

思路

递归方法

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> inorderTraversal(TreeNode *root) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        vector<int> myvector;
        inorder(root,myvector);
        return myvector;  
        
    }
    void inorder(TreeNode *root, vector<int> &myvector)
    {
        if(!root)
            return ;
        inorder(root->left,myvector);
        myvector.push_back(root->val);
        inorder(root->right,myvector);
    }  
    
};

 

迭代方法

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution { 
public:
    vector<int> inorderTraversal(TreeNode *root) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        vector<int> myvector;
        stack<TreeNode *> mystack;
        while(!mystack.empty() || root!=NULL)
        {
            while(root)
            {
                mystack.push(root);
                root = root->left;                
            }
            if(!mystack.empty())
            {
                root = mystack.top();
                mystack.pop();
                myvector.push_back(root->val);  
                root = root->right;
            }
        }
        return myvector;    
    }  
    
};

 

最新 java

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<Integer>();
        if(root == null){
            return result;
        }
        
        Stack<TreeNode> stack = new Stack<TreeNode>();
        TreeNode p = root;
        // while(p != null || !stack.empty()){
        //     while(p != null){
        //         stack.push(p);
        //         p = p.left;
        //     }
        //     if(!stack.empty()){
        //         p = stack.pop();
        //         result.add(p.val);
        //         stack.push(p.right);
        //     }
        // }
        while(p != null || !stack.empty()){
            if(p != null){
                stack.push(p);
                p = p.left;
            } else {
                TreeNode t = stack.pop();
                result.add(t.val);
                p = t.right;
            }
        }
        return result;
    }
}

上述第一种超时。。。


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值