Java. Basic inorder DSF


  • 0
    Q
    public class Solution {
        public int kthSmallest(TreeNode root, int k) {
            this.k = k;
            iterate(root);
            return value;
        }
        int k = 0;
        int index = 0;
        int value;
        private void iterate(TreeNode node){
            if (node == null){
                return;
            }
            iterate(node.left);
            index++;
            if (index == k){
                value = node.val;
                return;
            }
            iterate(node.right);
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.