二叉树前序遍历C#实现

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     public int val;
 5  *     public TreeNode left;
 6  *     public TreeNode right;
 7  *     public TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution {
11     public IList<int> PreorderTraversal(TreeNode root) {
12         List<int> result=new List<int>();
13         if (root==null)
14             return result;
15         result.Add(root.val);
16         if(root.left!=null)
17             result.AddRange(PreorderTraversal(root.left));
18         if(root.right!=null)
19             result.AddRange(PreorderTraversal(root.right));
20         return result;
21     }
22 }

猜你喜欢

转载自www.cnblogs.com/wuwan/p/8970974.html