標籤:一、while迴圈執行個體:public class Test{ public static void main(String[] args){ int i = 1; while(i<30){ System.out.println(i); i++; } }}二、do-while迴圈public class Test{ public static void
標籤:Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary tree and sum = 22, 5 / 4
標籤:Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.解題思路:遞迴即可,JAVA實現如下: public
標籤:Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.解題思路:注意minimum depth最後遍曆的那個點,left right都必須為null,JAVA實現如下: public int
標籤:Given a binary tree and a sum, find all root-to-leaf paths where each path‘s sum equals the given sum.For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4
標籤:Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.解題思路:同上題,JAVA實現如下: public TreeNode sortedListToBST(ListNode head) { ArrayList<Integer> list=new ArrayList<Integer>()
標籤:Given a binary tree, return the bottom-up level order traversal of its nodes‘ values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its
標籤:Given an array where elements are sorted in ascending order, convert it to a height balanced BST.解題思路:首先要理解,什麼叫做height balanced BST Java for LeetCode 110 Balanced Binary Tree,然後就十分容易了,JAVA實現如下:public TreeNode sortedArrayToBST(int[] nums) {return
標籤:Construct Binary Tree from Inorder and Postorder TraversalTotal Accepted: 31041 Total Submissions: 115870 Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in