(Tree)94.Binary Tree Inorder Traversal

Posted cKK

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(Tree)94.Binary Tree Inorder Traversal相关的知识,希望对你有一定的参考价值。

/**
 * 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();
        ArrayDeque<TreeNode> stack=new ArrayDeque();  //双端队列的使用
        TreeNode tmp=root;
        while(!stack.isEmpty() || tmp!=null){       //双判断
            if(tmp!=null){
                stack.push(tmp);
                tmp=tmp.left;
            }
            else{
                tmp=stack.pop();
                res.add(tmp.val);
                tmp=tmp.right;
            }
        }
        return res;
    }
}

  

以上是关于(Tree)94.Binary Tree Inorder Traversal的主要内容,如果未能解决你的问题,请参考以下文章

94. Binary Tree Inorder Traversal

94. Binary Tree Inorder Traversal

#Leetcode# 94. Binary Tree Inorder Traversal

94. Binary Tree Inorder Traversal

94. Binary Tree Inorder Traversal

94. Binary Tree Inorder Traversal