大家好,又见面了,我是全栈君。
反转二叉树(就是二叉树的镜像)
public class Mirror {
public void mirrorTree(TreeNode root) {
if (null == root) {// 空结点
return;
}
if (root.left == null && root.right == null) {// 叶子结点或者根结点
return;
}
TreeNode temp = null;
temp = root.left;
root.left = root.right;
root.right = temp;
if (root.left != null) {
mirrorTree(root.left);
}
if (root.right != null) {
mirrorTree(root.right);
}
}
}
二进制数中1的个数(用与运算)
static int NumberOf(int n) {
int count=0;
while(n!=0){//整数不为0,必有1
++count;
n=n&(n-1);
}
return count;
}
转载于:https://my.oschina.net/gaomq/blog/1627759
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/107687.html原文链接:https://javaforall.net