2016/11/9
public class Solution {
void find(TreeNode pRoot, int k,TreeNode[] result,int[] count)
{
if(result[0]!= null||pRoot==null)
{return;}
find(pRoot.left,k,result,count);
count[0]++;
if(count[0]==k)
{ result[0]=pRoot ;}
find(pRoot.right,k,result,count);
}
TreeNode KthNode(TreeNode pRoot, int k)
{
if(pRoot == null || k <= 0){
return null;
}
TreeNode[] result = new TreeNode[1];
//TreeNode u=null;
find(pRoot,k,result,new int[1]);
return result[0];
}
}
1.在写函数void find(TreeNode pRoot, int k,TreeNode[] result,int[] count)时发现不可以直接使用int变量,需要初始化一个into[],需要初始化一个内存空间来存储变量。
2.函数void find(TreeNode pRoot, int k,TreeNode[] result,int[] count)
if(result[0]!= null||pRoot==null)
{return;}
这个条件写在最开始可以减少树的遍历,提高效率,如果在左子树已经发现了该结点,右子树就不用再被访问了。