Sink All Zero
Question
Solution
public TreeNode sinkAllZero(TreeNode root) {
if(root==null) return root;
sinkAllZero(root.left);
sinkAllZero(root.right);
sink(root);
return root;
}
private void sink(TreeNode root) {
// in this time, root.val = 0.
if(root.val==1) return;
if((root.left==null || root.left.val==0) && (root.right==null || root.right.val==0)) return;
if(root.left!=null && root.left.val==1) {
root.left.val = 0;
root.val = 1;
sink(root.left);
}else {
root.right.val = 0;
root.val = 1;
sink(root.right);
}
}Last updated