LeetCode基础--二叉树--中序遍历

中序: 左->根->右

递归实现:

public class Solution {
    public IList<int> PreorderTraversal(TreeNode root) {
        List<int> result = new List<int>();
        if (root != null){
            result.AddRange(PreorderTraversal(root.left));
            result.Add(root.val);
            result.AddRange(PreorderTraversal(root.right));
        }
        return result;
    }
}

非递归实现:

public class Solution {
    public IList<int> InorderTraversal(TreeNode root) {
        List<int> result = new List<int>();
        if(root == null)
        {
            return result;
        }


        Stack stack = new Stack();
        TreeNode runNode = root;
        while(runNode != null || stack.Count > 0)
        {
            if(runNode != null)
            {
                stack.Push(runNode);
                runNode = runNode.left;                
            }
            else
            {
                runNode = stack.Peek();
                result.Add(stack.Pop().val);
                runNode = runNode.right;
            }

        }

        return result;
    }
}

你可能感兴趣的:(LeetCode)