二叉搜索树的第K大节点

xiaoxiao2021-02-28  125

class Solution{ int index= 0; TreeNode node =null; TreeNode KthNode(TreeNode pRoot, int k) { midSearch(pRoot,k); return node; } void midSearch(TreeNode root,int k){ if(root!=null){ midSearch(root.left,k); index++; if(index==k){ node = root; return; } midSearch(root.right,k); } } public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } }
转载请注明原文地址: https://www.6miu.com/read-36694.html

最新回复(0)