二叉树的中序遍历

class Solution {
    public List inorderTraversal(TreeNode root) {
        List list = new ArrayList<>();
        Stack stack = new Stack<>();
        TreeNode currentNode = root;
        while(currentNode!=null || !stack.isEmpty()){
            if(currentNode!=null){
                stack.push(currentNode);
                currentNode = currentNode.left;
            }else{
                currentNode = stack.pop();
                list.add(currentNode.val);
                currentNode = currentNode.right;
            }    
        }
        return list;
    }
}

 

你可能感兴趣的:(数据结构)