File tree Expand file tree Collapse file tree 3 files changed +91
-0
lines changed
Expand file tree Collapse file tree 3 files changed +91
-0
lines changed Original file line number Diff line number Diff line change 1+
2+
3+ import java .util .HashMap ;
4+
5+ public class LeetCode_1_136 {
6+
7+ public int [] twoSum (int [] nums , int target ) {
8+ HashMap <Integer , Integer > map = new HashMap ();
9+ for (int i = 0 ; i < nums .length ; i ++) {
10+ map .put (Integer .valueOf (nums [i ]), i );
11+ }
12+
13+ for (int i = 0 ; i < nums .length ; i ++) {
14+ Integer index = map .get (target - nums [i ]);
15+ if (index != null && index .intValue () != i ) {
16+ return new int []{i , index .intValue ()};
17+ }
18+ }
19+ return null ;
20+ }
21+
22+ public static void main (String [] args ) {
23+ int [] ns = {2 ,7 ,11 ,15 };
24+
25+ int [] ints = new LeetCode_1_136 ().twoSum (ns , 9 );
26+ for (int i = 0 ; i < ints .length ; i ++) {
27+ System .out .println (i + "," );
28+ }
29+ }
30+ }
Original file line number Diff line number Diff line change 1+ import java .util .ArrayList ;
2+ import java .util .Arrays ;
3+
4+ public class lowestCommonAncestorOfABinaryTree {
5+ public TreeNode lowestCommonAncestor (TreeNode root , TreeNode A , TreeNode B ) {
6+ if (root == null || root == A || root == B ) {
7+ return root ;
8+ }
9+ TreeNode left = lowestCommonAncestor (root .left , A , B );
10+ TreeNode right = lowestCommonAncestor (root .right , A , B );
11+ if (left != null && right != null ) {
12+ return root ;
13+ }
14+
15+ if (left != null ) {
16+ return left ;
17+ }
18+
19+ if (right != null ) {
20+ return right ;
21+ }
22+ return null ;
23+ }
24+ }
Original file line number Diff line number Diff line change 1+
2+
3+ import java .util .ArrayList ;
4+ import java .util .List ;
5+ import java .util .Stack ;
6+
7+ public class LeetCode_94_136 {
8+
9+ public List <Integer > inorderTraversal (TreeNode root ) {
10+ List <Integer > l = new ArrayList <Integer >();
11+ Stack <TreeNode > stack = new Stack ();
12+ TreeNode node = root ;
13+
14+ while (!stack .isEmpty () || node != null ) {
15+ if (node != null ) {
16+ stack .push (node );
17+ node = node .left ;
18+ } else {
19+ if (!stack .isEmpty ()) {
20+ node = stack .pop ();
21+ l .add (node .val );
22+ node = node .right ;
23+ }
24+ }
25+ }
26+ return l ;
27+ }
28+ }
29+ class TreeNode {
30+ int val ;
31+ TreeNode left ;
32+ TreeNode right ;
33+
34+ TreeNode (int x ) {
35+ val = x ;
36+ }
37+ }
You can’t perform that action at this time.
0 commit comments