A transaction is a segwit tx if at least one of the inputs contain a witness. Or if you are inspecting the raw tx then you check the 5th byte (the input count) and if it is 0x00
then it is a segwit tx.
Example:
tx1
02000000055f4e5315ffe854e75db92f8dda952a8db2f63b37838fca4c32bd4bb692fe7ea2000000006a47304402200ab62a9d1858dceeaa2101cda3f31b7bae60471c0355c54a6eae48f723555806022010d2aad6362a8c56ed66b0670ff84175f534f4f0995f8724a9b2a5fc89a2a4b4012102c424c2670a7aade9cf867576064013d02cc7669c418968993e73e6b25fc122bcfeffffff80cb1bfcde0f309490c717eca07f1353f121fb13e105...
is not a segwit tx because the input count is 0x05
= 5
tx2
0100000000010106325bac2f2e7ca67fa46c8304fb3b747e5578df1eef0394349ce2cdd744f7f10100000000ffffffff02db355202000000001976a91489ea1263056ac068adba4844efb376a3a19635ad88ac43b72f07000000002...
is a segwit tx because the input count is 0x00
= 0
Also:
- Transactions spending P2PKH inputs (address starting with 1) cannot
be segwit.
- Transactions spending P2SH inputs will be segwit if they are nested
P2SH, otherwise not.
- Transactions spending P2WPKH or P2WSH (starting with bc1) will be
segwit.
In your example:
legacy -> P2SH
This cannot be a segwit tx because it spends inputs from a legacy address
P2SH -> legacy
This can be a segwit tx if the address is P2SH-P2WPKH or P2SH-P2WSH (nested segwit into P2SH). You wouldn't know if it is nested if it's not your's unless someone has already spent from it (in which case you inspect the spending tx).
bech32 -> legacy
This is always a segwit tx