Posts

590 - N-ary Tree Postorder Traversal

 JAVA class Solution { public List < Integer > postorder ( Node root ) { LinkedList < Integer > ans = new LinkedList <>(); if ( root == null ) { return ans ; } Deque < Node > stk = new ArrayDeque <>(); stk . offer ( root ); while (! stk . isEmpty ()) { root = stk . pollLast (); ans . addFirst ( root . val ); for ( Node child : root . children ) { stk . offer ( child ); } } return ans ; } } C++ class Solution { public: vector < int > postorder ( Node * root ) { vector < int > ans ; if ( ! root ) return ans ; stack < Node *> stk { { root } }; while ( ! stk . empty ()) { root = stk . top (); ans . push_back ( root -> val ); stk . pop (); ...

145. Binary Tree Postorder Traversal

 JAVA class Solution { public List < Integer > postorderTraversal ( TreeNode root ) { LinkedList < Integer > ans = new LinkedList <>(); while ( root != null ) { if ( root . right == null ) { ans . addFirst ( root . val ); root = root . left ; } else { TreeNode next = root . right ; while ( next . left != null && next . left != root ) { next = next . left ; } if ( next . left == null ) { ans . addFirst ( root . val ); next . left = root ; root = root . right ; } else { next . left = null ; root = root . left ; } } } return ans ; } } C++ class Solution { public: ...

564 - Find the Closest Palindrome

 JAVA class Solution { public String nearestPalindromic ( String n ) { long x = Long . parseLong ( n ); long ans = - 1 ; for ( long t : get ( n )) { if ( ans == - 1 || Math . abs ( t - x ) < Math . abs ( ans - x ) || ( Math . abs ( t - x ) == Math . abs ( ans - x ) && t < ans )) { ans = t ; } } return Long . toString ( ans ); } private Set < Long > get ( String n ) { int l = n . length (); Set < Long > res = new HashSet <>(); res . add (( long ) Math . pow ( 10 , l - 1 ) - 1 ); res . add (( long ) Math . pow ( 10 , l ) + 1 ); long left = Long . parseLong ( n . substring ( 0 , ( l + 1 ) / 2 )); for ( long i = left - 1 ; i <= left + 1 ; ++ i ) { StringBuilder sb = new StringBui...

476 - Number Complement

 JAVA class Solution { public int findComplement ( int num ) { int ans = 0 ; boolean find = false ; for ( int i = 30 ; i >= 0 ; -- i ) { int b = num & ( 1 << i ); if (! find && b == 0 ) { continue ; } find = true ; if ( b == 0 ) { ans |= ( 1 << i ); } } return ans ; } } C++ class Solution { public: int findComplement ( int num ) { int ans = 0 ; bool find = false ; for ( int i = 30 ; i >= 0 ; -- i ) { int b = num & ( 1 << i ); if ( ! find && b == 0 ) continue ; find = true ; if ( b == 0 ) ans |= ( 1 << i ); } return ans ; } };

1140 - Stone Game II

 JAVA class Solution { private int [] s ; private Integer [][] f ; private int n ; public int stoneGameII ( int [] piles ) { n = piles . length ; s = new int [ n + 1 ]; f = new Integer [ n ][ n + 1 ]; for ( int i = 0 ; i < n ; ++ i ) { s [ i + 1 ] = s [ i ] + piles [ i ]; } return dfs ( 0 , 1 ); } private int dfs ( int i , int m ) { if ( m * 2 >= n - i ) { return s [ n ] - s [ i ]; } if ( f [ i ][ m ] != null ) { return f [ i ][ m ]; } int res = 0 ; for ( int x = 1 ; x <= m * 2 ; ++ x ) { res = Math . max ( res , s [ n ] - s [ i ] - dfs ( i + x , Math . max ( m , x ))); } return f [ i ][ m ] = res ; } } C++ class Solution { public: int stoneGameII ( vector < i...