C# Solution


  • 0
        public IList<int> InorderTraversal(TreeNode root) {
            List<int> result = new List<int>();
            Stack<TreeNode> stack = new Stack<TreeNode>();
            TreeNode currentNode = root;
    
            while (currentNode != null || stack.Count != 0)
            {
                while (currentNode != null)
                {
                    stack.Push(currentNode);
                    currentNode = currentNode.left;
                }
                
                if (stack.Count != 0)
                {
                    currentNode = stack.Pop();
                    result.Add(currentNode.val);
                    currentNode = currentNode.right;
                }
            }
                
            return result;
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.