标签:tps http ice coding pre style interview nod ret
二叉树的镜像定义:源二叉树 8 / 6 10 / \ / 5 7 9 11 镜像二叉树 8 / 10 6 / \ / 11 9 7 5
题目链接:
https://www.nowcoder.com/practice/564f4c26aa584921bc75623e48ca3011?tpId=13&tqId=11171&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
package com.sunshine.OFFER66_SECOND; public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } }
package com.sunshine.OFFER66_SECOND; import org.junit.Test; public class A18_Mirror { @Test public void test(){ TreeNode n1 =new TreeNode(1); TreeNode n2 =new TreeNode(2); TreeNode n3 =new TreeNode(3); TreeNode n4 =new TreeNode(4); TreeNode n5 =new TreeNode(5); TreeNode n6 =new TreeNode(6); TreeNode n7 =new TreeNode(7); n1.left=n2; n1.right=n3; n2.left=n4; n2.right=n5; n3.left=n6; n3.right=n7; Mirror(n1); TreeUtility.printTreeOfLine(n1); } public void Mirror(TreeNode root) { if(root == null ){ return; } TreeNode right = root.right; root.right = root.left; root.left = right; Mirror(root.left); Mirror(root.right); } }
标签:tps http ice coding pre style interview nod ret
原文地址:https://www.cnblogs.com/MoonBeautiful/p/11428879.html