二叉搜索树(BST)中的两个节点的值被错误地交换了, 请在不改变树的结构的情况下恢复这棵树。 备注; 用O(n)的空间解决这个问题的方法太暴力了,你能设计一个常数级空间复杂度的算法么? 示例 1: 输入: [1,3,null,null,2] 1 3 \ 2 输出: [3,1,null,null,2] 3 1 \ 2
加载中...
import java.util.*; /** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public void recoverTree(TreeNode root) { } }
/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: void recoverTree(TreeNode *root) { } };
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # # @param root TreeNode类 # @return void # class Solution: def recoverTree(self , root ): # write code here
/* * function TreeNode(x) { * this.val = x; * this.left = null; * this.right = null; * } */ /** * * @param root TreeNode类 * @return void */ function recoverTree( root ) { // write code here } module.exports = { recoverTree : recoverTree };
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # # @param root TreeNode类 # @return void # class Solution: def recoverTree(self , root ): # write code here
package main import . "nc_tools" /* * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ /** * * @param root TreeNode类 * @return void */ func recoverTree( root *TreeNode ) { // write code here }
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ /** * * @param root TreeNode类 * @return void */ void recoverTree(struct TreeNode* root ) { // write code here }