897. 递增顺序查找树

地址:https://leetcode-cn.com/problems/increasing-order-search-tree/

/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     public $val = null;
 *     public $left = null;
 *     public $right = null;
 *     function __construct($value) { $this->val = $value; }
 * }
 */
class Solution {

    /**
     * @param TreeNode $root
     * @return TreeNode
     */
    function increasingBST($root) {
        $res = [];
        $this->helper($root,$res);
        $root = new TreeNode(0);
        $node = $root;
       while(!empty($res)){
           $val=array_shift($res);
            $node->right = new TreeNode($val);
            $node = $node->right;
        }
        return $root->right;
    }

    public function helper($root,&$res){
        if($root == null) return ;
        $this->helper($root->left,$res);
        $res[]=$root->val;
        $this->helper($root->right,$res);
    }
}

 

posted @ 2020-05-22 10:52  花花妹子。  阅读(123)  评论(0编辑  收藏  举报