/** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNode left; * public TreeNode right; * public TreeNode(int x) { val = x; } * } */ public class Solution { Stack<int> S = new Stack<int>(); private void inOrder(TreeNode node) { if (node != null) { if (node.left != null) { inOrder(node.left); } S.Push(node.val); if (node.right != null) { inOrder(node.right); } } } public IList<int> InorderTraversal(TreeNode root) { inOrder(root); var list = S.Reverse().ToList(); return list; } }
https://leetcode.com/problems/binary-tree-inorder-traversal/#/description
时间: 2024-11-08 22:24:11