GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode/code/lc114.java at master · mJackie/leetcode Expand file tree/ Copy pathlc114.javaMore file actions 31 lines (31 loc) · 945 Bytes / Copy pathlc114.javaFile metadata and controls31 lines (31 loc) · 945 Bytes 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 package code; /* * 114. Flatten Binary Tree to Linked List * 题意:二叉树转链表 * 难度:Medium * 分类:Tree, Depth-first Search * 思路:就是节点间连接换一下,理清思路就行了,类似中序遍历 * Tips:lc426 */ public class lc114 { public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } public void flatten(TreeNode root) { if(root==null) return; flatten(root.left); flatten(root.right); TreeNode origin_r = root.right; root.right = root.left; //右节点替换为左节点 TreeNode right_leaf = root; while(right_leaf.right!=null){ //右节点遍历到最后,把左节点接上 right_leaf = right_leaf.right; } right_leaf.right = origin_r; root.left = null; //别忘了把left设成null } } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |