2021-05-03 c#问题--2
leetcode c# Q897. Increasing Order Search Tree
参考他人练习,需勤练多看
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
List<int> unsortedRoots=new List<int>();
public TreeNode IncreasingBST(TreeNode root) {
//find out the values of the tree node
findValue(root);
//sort the list
unsortedRoots.Sort();
//init a new treenode object
TreeNode newRoot=new TreeNode(unsortedRoots[0]);
// declare variable
TreeNode node=newRoot;
//assign the values to the right
for(int i=1;i<unsortedRoots.Count;i++)
{
node.right=new TreeNode(unsortedRoots[i]);
node=node.right;
}
return newRoot;
}
private void findValue(TreeNode root)
{
if(root==null)
{
return;
}
unsortedRoots.Add(root.val);
if(root.left!=null)
{
findValue(root.left);
}
if(root.right!=null)
{
findValue(root.right);
}
}
}
5827



被折叠的 条评论
为什么被折叠?



