国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

劍指offer經典編程(十一)

2019-11-08 02:56:40
字體:
來源:轉載
供稿:網友

二叉樹的鏡像

操作給定的二叉樹,將其變換為源二叉樹的鏡像。

輸入描述:

二叉樹的鏡像定義:源二叉樹

8 / / 6 10 / / / / 5 7 9 11 鏡像二叉樹 8 / / 10 6 / / / / 11 9 7 5import java.util.*;/**public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; }}*/public class Solution { public void Mirror(TreeNode root) { /*非遞歸算法 if(root == null){ return; }else if(root.left==null&&root.right==null){ return; } Stack<TreeNode> stack = new Stack<TreeNode>(); stack.push(root); while (!stack.isEmpty()) { TreeNode node = stack.pop(); if (node.left != null || node.right != null) { TreeNode temp = node.left; node.left = node.right; node.right = temp; } if (node.left != null) stack.push(node.left); if (node.right != null) stack.push(node.right); } */ //遞歸求解 if(root == null){ return; }else if(root.left==null&&root.right==null){ return; } TreeNode temp = root.left; root.left = root.right; root.right = temp; if(root.left!=null){ Mirror(root.left); } if (root.right!=null){ Mirror(root.right); } }}
上一篇:HDU1878 歐拉回路

下一篇:html常用標簽

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 广宁县| 平安县| 松滋市| 即墨市| 大渡口区| 会理县| 枝江市| 岚皋县| 吕梁市| 诸城市| 荣昌县| 德惠市| 虞城县| 雷州市| 武鸣县| 磐安县| 句容市| 秀山| 托里县| 宜良县| 和顺县| 蕲春县| 玛曲县| 宁国市| 刚察县| 资阳市| 宣威市| 河南省| 祁阳县| 康乐县| 巴中市| 闸北区| 河间市| 崇信县| 读书| 泾川县| 阿尔山市| 富宁县| 格尔木市| 汝州市| 阿城市|