首页 > 其他 > 详细

94. Binary Tree Inorder Traversal

时间:2016-04-03 15:50:52      阅读:78      评论:0      收藏:0      [点我收藏+]
/**
 * 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> res=new ArrayList<Integer>();
        Stack<TreeNode> st=new Stack<TreeNode>();
        TreeNode temp=root;
        while(temp!=null||!st.isEmpty())
        {
            // 将所有的节点都入栈,并且按照中序遍历的逆序进行入栈
            if(temp==null)
            {
                temp=st.pop();
                res.add(temp.val);
                temp=temp.right;
            }
            
            else
            {
                st.push(temp);
                temp=temp.left;
            }
            
        }
        return res;
    }
}

 

94. Binary Tree Inorder Traversal

原文:http://www.cnblogs.com/aguai1992/p/5349907.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!